package com.example.mvpframe.mvplibrary.ui.inject;

import android.content.Context;

import com.tchhy.mvplibrary.presenter.BaseActivityPresenter;
import com.tchhy.mvplibrary.ui.activity.BaseActivity;
import com.tchhy.mvplibrary.ui.fragment.BaseFragment;
import com.tchhy.mvplibrary.ui.fragment.BaseMvpFragment;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 简化presenter实例化，mRootView和lifecycleProvider的赋值
 */
public class InjectUtils {


    public static void injectActivity(Context context){


        Class<?> clazz = context.getClass();

        InitPresenter initPresenter = clazz.getAnnotation(InitPresenter.class);

        if(initPresenter!=null){
            try {
                Class<?> values = initPresenter.values();

                Field field = context.getClass().getField("mPresenter");


                boolean accessible = field.isAccessible();
                field.setAccessible(true);

                Object newPresenter = values.getConstructor(BaseActivity.class).newInstance(context);
                field.set(context, newPresenter);
                field.setAccessible(accessible);


                Field rootViewField = newPresenter.getClass().getField("mRootView");
                rootViewField.setAccessible(true);
                rootViewField.set( newPresenter,context);


                Field lifecycleField = newPresenter.getClass().getField("lifecycleProvider");
                lifecycleField.setAccessible(true);
                lifecycleField.set( newPresenter,context);


            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }


    }
    public static void injectFragment(BaseMvpFragment context){


        Class<?> clazz = context.getClass();

        InitPresenter initPresenter = clazz.getAnnotation(InitPresenter.class);

        if(initPresenter!=null){
            try {
                Class<?> values = initPresenter.values();

                Field field = context.getClass().getField("mPresenter");


                boolean accessible = field.isAccessible();
                field.setAccessible(true);

                Object newPresenter = values.getConstructor(BaseFragment.class).newInstance(context);
                field.set(context, newPresenter);
                field.setAccessible(accessible);


                Field rootViewField = newPresenter.getClass().getField("mRootView");
                rootViewField.setAccessible(true);
                rootViewField.set( newPresenter,context);

                Field lifecycleField = newPresenter.getClass().getField("lifecycleProvider");
                lifecycleField.setAccessible(true);
                lifecycleField.set( newPresenter,context);

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }


    }
}
